//
//  ParticipationSubViewController.m
//  homeland
//
//  Created by rain on 14-5-12.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "ParticipationSubViewController.h"
#import "ParticipateService.h"
#import "TKContactsMultiPickerController.h"

#define NUMBERS @"0123456789,，\n"

@interface ParticipationSubViewController () <ParticipateServiceDelegate,TKContactsMultiPickerControllerDelegate>
@property (nonatomic, weak) IBOutlet UIView *backView;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *datas;

@end

@implementation ParticipationSubViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"参与有奖";

    self.datas = [[NSMutableArray alloc] init];
    // Do any additional setup after loading the view from its nib.
    UIColor *color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2f];
    self.backView.layer.masksToBounds = YES;
    self.backView.layer.cornerRadius = 8.0f;
    self.backView.layer.borderWidth = 0.5f;
    self.backView.layer.borderColor = [color CGColor];
    
    int height = iPhone5 ? 0 : 88;

    UIButton *subBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 460-height, 320, 44)];
    [subBtn setBackgroundImage:[UIImage imageNamed:@"btnImage.png"] forState:0];
    [subBtn setTitle:@"提交" forState:0];
    [subBtn addTarget:self action:@selector(subCommender) forControlEvents:64];
    [self.view addSubview:subBtn];
    
    self.navigationItem.leftBarButtonItem = autoavBackButton(self, @selector(backMethod));
    [ParticipateService share].delegate = self;

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    int height = iPhone5 ? 380 : 290;
    [self.backView setFrame:CGRectMake(10, 65, 300, height)];
    [self.tableView setFrame:CGRectMake(0, 0, CGRectGetWidth(self.backView.frame), CGRectGetHeight(self.backView.frame))];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
}

- (IBAction)goAddressList:(id)sender
{
    TKContactsMultiPickerController *vc = [[TKContactsMultiPickerController alloc] initWithNibName:@"TKContactsMultiPickerController" bundle:nil];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)finishWithTel:(NSString *)tel name:(NSString *)name
{
    if (nil != tel) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:tel forKey:@"tel"];
        [dict setObject:nil == name ? tel : name forKey:@"name"];
        [self.datas addObject:dict];
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        [self.tableView reloadData];
    }
}

- (void)contactsMultiPickerController:(TKContactsMultiPickerController*)picker didFinishPickingDataWithInfo:(NSArray*)data
{
    
}

- (void)contactsMultiPickerControllerDidCancel:(TKContactsMultiPickerController*)picker
{
    
}

- (void)subCommender
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (NSMutableDictionary *dict in self.datas) {
        [array addObject:dict[@"tel"]];
    }
    NSString *string = [array componentsJoinedByString:@","];

    NSLog(@"string = %@",string);
    if ([string length] > 0) {
        [[ParticipateService share] subRecommender:string];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请输入推荐的电话号码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

#pragma mark tableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        UIButton *btn = [[UIButton alloc] init];
        [btn setFrame:CGRectMake(250, 20, 20, 20)];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"list_delete.png"] forState:0];
        
        btn.tag = indexPath.row + 1000;
        [btn addTarget:self action:@selector(deleTel:) forControlEvents:64];
        [cell.contentView addSubview:btn];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    NSMutableDictionary *dict = self.datas[indexPath.row];
    [cell setBackgroundColor:[UIColor clearColor]];
    [cell.contentView setBackgroundColor:[UIColor clearColor]];
    cell.textLabel.text = dict[@"name"];
    cell.detailTextLabel.text = dict[@"tel"];
    UIColor *color = [UIColor colorWithRed:250/255.0f green:243/255.0f blue:225/255.0f alpha:0.2f];
    cell.layer.masksToBounds = YES;
    cell.layer.borderWidth = 0.5f;
    cell.layer.borderColor = [color CGColor];

    return cell;
}

- (void)subSuccess:(BOOL)isSuccess
{
    if (isSuccess) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)deleTel:(UIButton *)btn
{
    [self.datas removeObjectAtIndex:btn.tag - 1000];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
